package com.wswy.wzcx.ui.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter;
import com.thoughtbot.expandablerecyclerview.models.ExpandableGroup;
import com.wswy.wzcx.R;
import com.wswy.wzcx.model.DataCenter;
import com.wswy.wzcx.model.Genre;
import com.wswy.wzcx.model.GenreChild;
import com.wswy.wzcx.ui.hold.GenreViewHolder;
import com.wswy.wzcx.ui.hold.ArtistViewHolder;
import com.wswy.wzcx.ui.other.ChildClickListener;

import java.util.Arrays;
import java.util.List;

/**
 * Created by wangduheng26 on 2018/1/23.
 * wangduheng26@gmail.com
 */

public class GenreAdapter extends ExpandableRecyclerViewAdapter<GenreViewHolder, ArtistViewHolder> {
    List<String> contents;
    int[] icons;
    ChildClickListener childListener;
    protected List<? extends ExpandableGroup> groups;
   public int  getVisibleItemCount(){
       return expandableList.getVisibleItemCount();
   }

    public GenreAdapter(List<? extends ExpandableGroup> groups) {
        super(groups);
        this.groups = groups;
        contents = Arrays.asList(DataCenter.get().contents);
        icons = DataCenter.get().icons;
        firstShow = true;
    }

    public void changed() {
        boolean[] indexes = expandableList.expandedGroupIndexes;
        if(indexes.length!=groups.size()){
            expandableList.expandedGroupIndexes = new boolean[groups.size()];
            for (int i = 0; i < groups.size(); i++) {
                if(i==0){
                    expandableList.expandedGroupIndexes[i] = true;
                }else{
                    expandableList.expandedGroupIndexes[i] = false;
                }
            }
        }

        notifyDataSetChanged();
    }

    public void setChildClickListener(ChildClickListener listener) {
        childListener = listener;
    }

    @Override
    public GenreViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
        return new GenreViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bill_group, parent, false));
    }

    @Override
    public ArtistViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
        ArtistViewHolder artistViewHolder = new ArtistViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bill_child, parent, false));
        artistViewHolder.setOnClickListener(childListener);
        return artistViewHolder;
    }

    @Override
    public int getItemCount() {
        try {
            return expandableList.getVisibleItemCount();
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public void onBindChildViewHolder(ArtistViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
        if (group instanceof Genre) {
            List<GenreChild> items = ((Genre) group).getItems();
            if (items != null) {
                final int size = items.size();
                if (childIndex >= 0 && childIndex < size) {
                    GenreChild child = items.get(childIndex);
                    holder.bind(child);
                    if (contents != null) {
                        int i = contents.indexOf(child.title);
                        holder.imageView.setImageResource(icons[i]);
                        holder.imageView.setSelected(true);
                    }
                }
            }
        }
    }

    boolean firstShow;

    @Override
    public void onBindGroupViewHolder(final GenreViewHolder holder, int flatPosition, ExpandableGroup group) {
        holder.bind(group);
        holder.imageArrow.setSelected(isGroupExpanded(group));
        if (flatPosition == 0 && firstShow) {
            holder.itemView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    boolean groupExpanded = isGroupExpanded(0);
                    if (!groupExpanded) {
                        toggleGroup(0);
                        holder.expand();
                        firstShow = false;
                    }
                }
            }, 200);
        }

    }


}
